// pages/catetory/index.js
import { $http } from '../../request/index-api'
import regeneratorRuntime from '../../lib/runtime';
Page({


  /**
   * 页面的初始数据
   */
  data: {
    menuTitle: [],
    menuContent: [],
    current: 0,
    resetScroll: 0
  },
  menuData: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //制作缓存
    const result = wx.getStorageSync('cate');
    if (!result) {
      this.getMenu();
    } else {
      if (Date.now()-result.time > 1000 * 60*5) {
        this.getMenu();
      } else {
        this.menuData = result.data
        let menuTitle = this.menuData.map((v => v.cat_name))
        //获取右边的所有数据
        const menuContent = this.menuData[0].children
        this.setData({
          menuTitle,
          menuContent
        })
      }
    }

  },
  async getMenu () {
    const result = await $http({ url: '/categories' })
    //整个分类的所有数据
    this.menuData = result
    //给本地存储放入数据
    wx.setStorageSync('cate', { time: Date.now(), data: this.menuData });
    //获取标题所有数据
    let menuTitle = this.menuData.map((v => v.cat_name))
    //获取右边的所有数据
    const menuContent = this.menuData[0].children
    this.setData({
      menuTitle,
      menuContent
    })
  }
  //点击切换类型，切换索引
  , cutoverMenu (e) {
    const { index } = e.target.dataset
    const menuContent = this.menuData[index].children
    this.setData({
      current: index,
      menuContent,
      resetScroll: 0
    })
  }
})